package com.lv.common.widget.video_editor

/**
 * create by libo
 * create on 2018/12/20
 * description 播放器VideoPlayer
 */
//class VideoPlayer @JvmOverloads constructor(context: Activity, attrs: AttributeSet? = null) :
//    FrameLayout(context, attrs), Iplayer, DefaultLifecycleObserver {
//
//        private val trackSelector: TrackSelector = DefaultTrackSelector(context)
//    private val mPlayer: MyPlayerView by lazy {
//        SimpleExoPlayer.Builder(context)
//            .setTrackSelector(trackSelector)
////                .setLoadControl(loadControl) //构建内存缓冲
//            .build()
////        ExoPlayerFactory.newSimpleInstance(context,
////            trackSelector,
////            DefaultLoadControl()
////        )
//    }
////    private var orientationUtils: OrientationUtils? = null
////    private var mContext: Activity = context
//
//    // 自定义 DefaultLoadControl 参数
//    val MIN_BUFFER_MS = 5_000 // 最小缓冲时间，
//    val MAX_BUFFER_MS = 7_000 // 最大缓冲时间
//    val PLAYBACK_BUFFER_MS = 700 // 最小播放缓冲时间，只有缓冲到达这个时间后才是可播放状态
//    val REBUFFER_MS = 1_000 // 当缓冲用完，再次缓冲的时间
////    val loadControl = DefaultLoadControl.Builder()
////        .setPrioritizeTimeOverSizeThresholds(true)//缓冲时时间优先级高于大小
////        .setBufferDurationsMs(MIN_BUFFER_MS, MAX_BUFFER_MS, PLAYBACK_BUFFER_MS, REBUFFER_MS)
////        .build()
//
//    private var binding: ViewPlayviewBinding =
//        ViewPlayviewBinding.inflate(LayoutInflater.from(context), this, true)
//
//    companion object {
//        const val MAX_CACHE_BYTE: Long = 1024 * 1024 * 200  //200MB
//    }
//
//    init {
//        initPlayer()
//    }
//
//    override fun onResume(owner: LifecycleOwner) {
//        super.onResume(owner)
//
//        //返回时，推荐页面可见，则继续播放视频
////        if (MainActivity.curMainPage == 0 && MainFragment.Companion.curPage == 1) {
////            play()
////        }
//    }
//
//    override fun onPause(owner: LifecycleOwner) {
//        super.onPause(owner)
//
//        pause()
//    }
//
//    override fun onStop(owner: LifecycleOwner) {
//        super.onStop(owner)
//
//        stop()
//    }
//
//    override fun onDestroy(owner: LifecycleOwner) {
//        super.onDestroy(owner)
//
//        release()
//    }
//
//    private fun initPlayer() {
////        //外部辅助的旋转，帮助全屏
////        orientationUtils = OrientationUtils(mContext, binding.playerview)
////        //初始化不打开外部的旋转
////        orientationUtils?.isEnable = false
////        val gsyVideoOption = GSYVideoOptionBuilder()
//////        mBinding.fullscreen.setOnClickListener { //直接横屏
////        binding.playerview.fullscreenButton.setOnClickListener { //直接横屏
////            orientationUtils?.resolveByClick()
////
////            //第一个true是否需要隐藏actionbar，第二个true是否需要隐藏statusbar
////            binding.playerview.startWindowFullscreen(mContext, false, false)
////        }
//
////        val imageView = ImageView(mContext)
////        imageView.adjustViewBounds = true
////        imageView.scaleType = ImageView.ScaleType.CENTER_CROP
////        imageView.adjustViewBounds = true
////        imageView.load(it.imageFirst)
////                    val drawable: Drawable = resources.getDrawable(com.lv.common.R.drawable.bg_color_theme_5)
////                    mBinding.ygxVideo.loadCoverImage(it.imageFirst,0)
////        val url = "https://wave.video/embed/6581eb1700de0e0d40e8edf6/6581eb1e75b04937fe24de74.mp4";
////        gsyVideoOption
//////            .setThumbImageView(imageView)
////            .setThumbPlay(true)
////            .setIsTouchWiget(true)
////            .setRotateViewAuto(false)
////            .setLockLand(true)
////            .setAutoFullWithSize(true)
////            .setShowFullAnimation(true)
////            .setNeedLockFull(true)
////            .setUrl(url)
////            .setCacheWithPlay(true)
////            .setDismissControlTime(990000000)
//////                        .setBottomProgressBarDrawable(drawable)
////            .setEnlargeImageRes(com.lv.common.R.drawable.icon_full_srceen)
////            .setShrinkImageRes(com.lv.common.R.drawable.icon_full_srceen)
//////                        .setVideoTitle("测试视频")
////            .setVideoAllCallBack(object : GSYSampleCallBack() {
////                override fun onPrepared(url: String, vararg objects: Any) {
////                    super.onPrepared(url, *objects)
////                    //开始播放了才能旋转和全屏
////                    orientationUtils?.isEnable = true
//////                    isPlay = true
////                }
////
////                override fun onQuitFullscreen(url: String, vararg objects: Any) {
////                    super.onQuitFullscreen(url, *objects)
////                    Debuger.printfError("***** onQuitFullscreen **** " + objects[0]) //title
////                    Debuger.printfError("***** onQuitFullscreen **** " + objects[1]) //当前非全屏player
////                    orientationUtils?.backToProtVideo()
////                    LogUtils.e("{onQuitFullscreen${objects[0] == objects[1]} }")
////
////                }
////            }).setLockClickListener { view, lock ->
////                //配合下方的onConfigurationChanged
////                orientationUtils?.isEnable = !lock
////            }.build(binding.playerview)
//////                    mBinding.ygxVideo.startAfterPrepared()
////        binding.playerview.startButton.performClick()
//
//
//        binding.playerview.player = mPlayer
//        binding.playerview.useController = false
//        mPlayer.playWhenReady = true
//        mPlayer.repeatMode = Player.REPEAT_MODE_ALL
//    }
//
//    /**
//     * 使用本地缓存播放
//     */
//    fun playVideo(mediaSource: BaseMediaSource) {
//        mPlayer.setMediaSource(mediaSource)
//        mPlayer.prepare()
//        mPlayer.play()
//    }
//
//
//    /**
//     * 构建一个共用缓存文件
//     */
//    val cache: SimpleCache by lazy {
//        val cacheFile = context.cacheDir.resolve("tiktok_cache_file")
//        SimpleCache(
//            cacheFile,
//            LeastRecentlyUsedCacheEvictor(MAX_CACHE_BYTE),
//            StandaloneDatabaseProvider(context)
//        )
//    }
//
//    /**
//     * 根据url生成缓存，播放本地缓存
//     */
//    override fun playVideo(url: String) {
//        if (TextUtils.isEmpty(url)) {
//            return
//        }
//        val mediaItem = MediaBrowser.MediaItem.fromUri(url)
//        val dataSourceFactory =
//            Kind.Factory().setCache(cache).setUpstreamDataSourceFactory(
//                Kind.Factory(context)
//            )
//        val mediaSource =
//            ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(mediaItem)
//        mPlayer.setMediaSource(mediaSource)
//        mPlayer.prepare()
//        mPlayer.play()
//    }
//
//    override fun getplayer(): MyPlayerView {
////        return binding.playerview
//        return mPlayer
//    }
//
//    override fun pause() {
//        mPlayer.pause()
//    }
//
//    override fun play() {
//        mPlayer.play()
//    }
//
//    override fun stop() {
//        mPlayer.stop()
//    }
//
//    override fun release() {
//        mPlayer?.let {
//            it.release()
//        }
//    }
//
//
////    override fun isPlaying(): Boolean = binding.playerview.playerState
//    override fun isPlaying(): Boolean = mPlayer.isPlaying
//}